﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace RDC.DATA.Class.Météo
{
    public class ApiMeteo
    {
        #region Champs
        private DateTime _date = DateTime.Now;


        private string _cp = "00000";


        private String _meteoNuage = "Météo Inconnue ";
        private String _meteoTemps = "Météo Inconnue";


        private String _meteoVent = "météo inconnue";



        private static String _getMeteoByCodePostal = "http://api.openweathermap.org/data/2.5/forecast/daily?q=#cp#&mode=xml&units=metric&cnt=7";
        #endregion

        #region propriétés

        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }

        public string Cp
        {
            get { return _cp; }
            set { _cp = value; }
        }
        public String MeteoNuage
        {
            get { return _meteoNuage; }
            set { _meteoNuage = value; }
        }
        public String MeteoVent
        {
            get { return _meteoVent; }
            set { _meteoVent = value; }
        }

        public String MeteoTemps
        {
            get { return _meteoTemps; }
            set { _meteoTemps = value; }
        }



        #endregion

        #region Constructeur

        public ApiMeteo()
        {
        }

        public ApiMeteo(string cp, DateTime date)
        {
            this.Cp = cp;
            this.Date = date;
        }

        #endregion


        #region Methodes

        public void getLaMeteo(string villeConcert, string dateConcert)
        {
            string urlMeteo = _getMeteoByCodePostal.Replace("#cp#", villeConcert);

            XElement xMeteo = XDocument.Load(urlMeteo).Root;

            IEnumerable<XElement> xTimes = xMeteo.Element("forecast").Elements("time");

            string MeteoNuage = " ";
            string MeteoTemps = "";
            string MeteoVent = "";

            foreach (XElement xUnTime in xTimes)
            {
                // "day=\"2013-12-12\"" 
                string date = "";

                date = xUnTime.Attribute("day").ToString().Remove(0, 7);
                date = date.Remove(8, 1);
                string year = "20" + date.Substring(0, 2);
                string month = date.Substring(3, 2);
                string day = date.Substring(6, 2); ;
                DateTime dateT = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));

                if (dateT == this.Date)
                {
                    XElement meteo = xUnTime.Element("clouds");
                    XElement meteo2 = xUnTime.Element("symbol");
                    XElement meteo3 = xUnTime.Element("windSpeed");

                    MeteoNuage = meteo.Attribute("value").Value.ToString();
                    MeteoTemps = meteo2.Attribute("name").Value.ToString();
                    MeteoVent = meteo3.Attribute("name").Value.ToString();
                }
            }



            this.MeteoNuage = MeteoNuage;
            this.MeteoTemps = MeteoTemps;
            this.MeteoVent = MeteoVent;




        }

        #endregion

    }
}
